# -*- coding: utf-8 -*-
import scrapy
import json
from ..items import GupiaoItem


class GplbSpider(scrapy.Spider):
    name = 'gplb'

    # allowed_domains = ['']
    # start_urls = ['']

    def start_requests(self):
        for tab in range(1, 4):
            yield scrapy.Request(
                url='http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1110&TABKEY=tab' + str(tab),
                callback=self.parse)

    def parse(self, response):
        response_data = json.loads(response.text)
        for d in response_data:
            if d['data'] == []:
                continue
            data = d['data']
            for i in data:
                zqdm = i['zqdm']
                gsjc = i['gsjc'][94:-8]
                sshymc = i['sshymc']
                print(gsjc)
                item = GupiaoItem()
                item['zqdm'] = zqdm
                item['gsjc'] = gsjc
                item['sshymc'] = sshymc
                yield item
